package com.ssh;

public class SwapPairs_24 {
	public ListNode swapPairs(ListNode head) {
		ListNode y = new ListNode(0);
		ListNode x = y;
		if (head == null) {
			return y.next;
		}
		x.next = head;
		while (head.next != null) {
			ListNode a = new ListNode(head.val);
			ListNode b = new ListNode(head.next.val);
			b.next = a;
			x.next = b;
			x = x.next.next;
			if (head.next.next != null) {
				head = head.next.next;
				ListNode c = new ListNode(head.val);
				x.next = c;
			}else {
				break;
			}
		}
		return y.next;
    }
	
	public ListNode swapPairs1(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode n = head.next;
		head.next = swapPairs1(head.next.next);
		n.next = head;
		return n;
	}
	
	
	public class ListNode {
		int val;
		ListNode next;
		ListNode(int x) { val = x; }
	}
}
